import requests
import user_agent_get
from pyquery import PyQuery as pq
from urllib.parse import urlencode
from time import sleep

def log_init():
    session=requests.Session()
    headers={"user-agent":user_agent_get.user_agent_get()
             ,"Host":"cas.pxc.jx.cn",
            "Sec-Fetch-User": '?1','Upgrade-Insecure-Requests': '1',
                'Upgrade-Insecure-Requests': '1',
    'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
             }
    return session,headers

def log_in_get(session,url,headers):
    # 登录到初始界面
    resp = session.get(url, headers=headers)
    tempPQ = pq(resp.text)
    # 获取post请求中的参数
    tempPQStr = tempPQ("input[type='hidden'][name='lt']").attr("value")
    tempPQStr22 = tempPQ("input[type='hidden'][name='execution']").attr("value")
    mylist=[tempPQStr,tempPQStr22]
    return session,mylist

def img_get(session,imgurl,headers):
    # 手动打码

    imgResp = session.get(imgUrl, headers=headers)
    with open(r'爬虫信息/a2.jpg', 'wb') as f:
        f.write(imgResp.content)
    return session

def login_post(session,data,posturl,mylist,headers):
    str2 = input("输入验证码: ")
    data.update({'captcha': str2, 'lt': mylist[0], 'execution': mylist[1]})

    data = urlencode(data)
    headers.update({'Referer': 'https://cas.pxc.jx.cn/lyuapServer/login', 'Host': 'cas.pxc.jx.cn'
                       , 'Origin': 'https://cas.pxc.jx.cn', 'Content-Type': 'application/x-www-form-urlencoded'})
    # 发送post请求
    sepost = session.post(url=posturl, headers=headers, data=data, allow_redirects=False)

    # 控制访问速度
    sleep(0.5)
    # 返回302的地址
    return sepost.headers['Location'],headers,session
def get_test(session,headers,url):
    #四次循环
    urlans=session.get(url=url,headers=headers,allow_redirects=False)
    # 控制访问速度
    sleep(0.5)
    return urlans.headers['Location'],session

def finall_get(session,headers,url):
    fina_1 = session.get(url=url, headers=headers,allow_redirects=False)
    mypq=pq(fina_1.text)
    print(mypq.html())
    with open("test8.html", 'w', encoding='utf-8') as f:
        f.write(fina_1.text)
    return session

if __name__ == '__main__':
    session,headers=log_init()

    url = "https://cas.pxc.jx.cn/lyuapServer/login"
    # 登录初始界面
    session,mylist=log_in_get(session,url,headers)


    # 手动打码
    imgUrl = "https://cas.pxc.jx.cn/lyuapServer/captcha.jsp"
    img_get(session,imgUrl,headers)


    #

    data = {
        'username': '211319060',
        'password': '000000',
        'captcha': '5454',
        'warn': 'true',
        'lt': 'LT-11972-eoF3b1RwEIudHnOjJTS112wz7HLfWA-cas01.example.org',
        'execution': 'e1s1',
        '_eventId': 'submit',
        'submit': '登录'
    }
    postUrl = "https://cas.pxc.jx.cn/lyuapServer/login"

    #发送post请求、表单
    url,headers,session=login_post(session, data, postUrl, mylist, headers)

    #四次验证
    # 1
    headers.update({"Host": "portal.pxc.jx.cn"})
    url,session=get_test(session,headers,url)


    #2
    headers.update({'Host': "cas.pxc.jx.cn", 'Upgrade-Insecure-Requests': '1'
                       , 'Cache-Control': 'max-age=0', 'Connection': 'keep-alive'})
    headers.pop('Referer')
    headers.pop('Sec-Fetch-User')
    headers.pop('Origin')
    url,session=get_test(session,headers,url)

    #3
    headers.update({"Host": "cas.pxc.jx.cn", 'Upgrade-Insecure-Requests': "1",
                    'Sec-Fetch-User': '?1'})
    url,session=get_test(session,headers,url)

    #4
    headers.update({"Host": "portal.pxc.jx.cn"})
    url,session=get_test(session,headers,url)

    #最后的返回页面
    session=finall_get(session,headers,url)

    #测试

    session2=requests.Session()
    url="http://10.110.1.33:8008/portal.cgi"
    headers.update({'Host': '10.110.1.33:8008','Origin': 'http://10.110.1.33:8008',
        'Referer': 'http://10.110.1.33:8008/portal/local/index.html?uplcyid=1&weburl=http%3A%2F%2Fjw.pxc.jx.cn%2Fjsxsd%2Fsso.jsp'
                    ,'X-Requested-With': 'XMLHttpRequest'})


    data={
        'username': '17379930923',
        'password': '930923',
        'uplcyid': '1',
        'language': '0',
        'submit': 'submit'
    }
    data=urlencode(data)
    postget=session2.post(url=url,headers=headers,data=data,allow_redirects=False)
    print("test PostGet :"+str(postget.status_code))
    print(postget.text)
    print(session2.headers)
    # print(session.cookies.get_dict())
    # session.cookies.pop("CASTGC")
    # session.cookies.pop("CASPRIVACY")
    # session.cookies.update({'SERVERID':'123'})
    # print(session.cookies.get_dict())



    # myheads={
    #     'JSESSIONID' : 'D8CCAFB817A445B65FBF736C2AD215E2',
    # '_gscu_93803120' : '367153693ncfaf63',
    # '_gscbrs_93803120' : '1',
    # 'SERVERID' : '123',
    # 'JSESSIONID' : '8A6929BCDD4DF28A5CD4CD3563157B01'
    # }
    # session.cookies.update(myheads)
    # print(session.cookies.get_dict())


    url='http://jw.pxc.jx.cn/jsxsd/sso.jsp '
    ssoget=session2.get(url,headers=headers,allow_redirects=False)
    print("ssoStatus: "+str(ssoget.status_code))
    print(ssoget.text)


    # url='http://jw.pxc.jx.cn/jsxsd/framework/xsMain.jsp'
    # headers.update({"Host":'jw.pxc.jx.cn','Upgrade-Insecure-Requests': '1',
    #                 'Referer': 'http://10.110.1.33:8008/'})
    # myans=session.get(url,headers=headers,allow_redirects=False)
    # print("myans stausCode :"+str(myans.status_code))
    # print('--------------------------')
    # print(myans.text)
    #
    #
    #
    # mypq=pq(myans.text)
    #
    # print(pq.html())
    # with open("test6.html",'w',encoding='utf-8') as ff:
    #     ff.write(pq.html())




